package com.vgooo.client.mobile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.vgooo.bean.Brand;
import com.vgooo.bean.ProdAbstract;
import com.vgooo.client.R;
import com.vgooo.util.BitmapUtils;
import com.vgooo.util.GSonTools;
import com.vgooo.util.HttpUtils;
import com.vgooo.util.UmengUtils;

public class MobileAdapter extends BaseAdapter {
	public final static Map<String, List<ProdAbstract>> prods = new HashMap<String, List<ProdAbstract>>();//缓存
	private static List<Brand> brands;// 品牌信息
	private List<ProdAbstract> prodsInfo ;//热门推荐手机列表
	private LayoutInflater inflater ;//视图加载器
	
	private  Context context ;
	
//	private Bi
	
	
	public void setProdsInfo(List<ProdAbstract> prodsInfo) {
		if (prodsInfo != null) {
			this.prodsInfo = prodsInfo;
		}else{
			prodsInfo = new ArrayList<ProdAbstract>();
		}
		
	}

	public MobileAdapter(List<ProdAbstract> prodsInfo, Context context) {
		// TODO Auto-generated constructor stub
		this.setProdsInfo(prodsInfo);
		this.context = context ;
		inflater = LayoutInflater.from(context);
	}
	/**
	 * 显示某个品牌的产品
	 * @param brand_id
	 * @throws IOException 
	 */
	public void dataChanage(final Brand brand) throws Exception{
		
		new AsyncTask<String, String, String>(){
			List<ProdAbstract> prods = null ;
			@Override
			protected String doInBackground(String... params) {
				// TODO Auto-generated method stub
				prods = MobileAdapter.prods.get(brand.brand_name);
				try {
					if(null==prods||prods.isEmpty()||prods.size()<1){
						String urlPath = HttpUtils.BASE_URI + "category?b=" + brand.brand_id ;
						prods = getProds(urlPath);
					} 
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					UmengUtils.onError(context, e.toString());
				}
				return null;
			}
			
			
			protected void onPostExecute(String result) {
				if(prods==null||prods.isEmpty()||prods.size()<1){
				}else{
					prodsInfo =prods;
					notifyDataSetChanged();
					MobileAdapter.prods.put(brand.brand_name, prodsInfo);
				}
			};
			
		}.execute("");
	}
	/**
	 * 显示搜索结果
	 * @param serachResult
	 */
	public void dataChanage(List<ProdAbstract> serachResult){
		if(serachResult != null){
			prodsInfo =serachResult;
			notifyDataSetChanged();
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if(prodsInfo==null||prodsInfo.isEmpty()){
			return 0 ;
		}else{
			return prodsInfo.size();
		}
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return prodsInfo.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ProdAbstract prod = prodsInfo.get(position);
		ViewHolder holder = null ;
		if(convertView==null){
			convertView = inflater.inflate(R.layout.moblie_item	, null);
			holder = new ViewHolder() ;
			convertView.setTag(holder);
			holder.image = (ImageView)convertView.findViewById(R.id.mobile_item_image);
			holder.name = (TextView)convertView.findViewById(R.id.mobile_item_name);
			holder.price = (TextView)convertView.findViewById(R.id.mobile_item_price);
//			holder.diver = (LinearLayout) convertView.findViewById(R.id.mobile_item_diver);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		
		
		holder.image.setVisibility(View.INVISIBLE);
		if(prod.isBlank){
			holder.image.setImageResource(R.drawable.bg_transparent);
			holder.name.setText("");
			holder.price.setText("");
			convertView.setClickable(false);
		}else{
			holder.image.setVisibility(View.VISIBLE);
			BitmapUtils.loaderImage(prod.prod_thumbnail, holder.image,context);
			holder.name.setText(prod.brand_name +" "+ prod.prod_name);
			holder.price.setText("￥"+prod.price_sell);
		}
		
		return convertView;
	}
	
	class ViewHolder{
		ImageView image ;
		TextView name ;
		TextView price ;
		LinearLayout diver ;
	}
	

	
	/**
	 * 打印调试信息
	 * 
	 * @param info
	 */
	private static void log(String info) {
		Log.i("MobileAdapter", info);
	}
	
	public static List<ProdAbstract> getProds(String urlPath) throws Exception{
		String jsonString = HttpUtils.getJsonString(urlPath);
		Map<String,Object> info = GSonTools.getObjectMaps(jsonString);
		Map<String,Object> brand = (Map<String, Object>) info.get("brand");
		String brand_html = (String) brand.get("brand_html");
		String brand_name = (String) brand.get("brand_name");
		List<Map<String, Object>> prods = (List<Map<String, Object>>) info.get("prods");
		List<ProdAbstract> prodInfos = new ArrayList<ProdAbstract>();
		for (Map<String, Object> map : prods) {
			ProdAbstract prod = new ProdAbstract();
			prod.brand_name = brand_name;
			prod.brand_html = brand_html;
			prod.prod_thumbnail = (String) map.get("prod_image");
			prod.prod_html = (String) map.get("prod_html");
			prod.prod_name = (String) map.get("prod_name");
			prod.price_sell = (String) map.get("prod_sell");
			prodInfos.add(prod);
			map = null ;
		}
		
		int s = 0 ;
		ProdAbstract prod = new ProdAbstract();
		prod.isBlank = true;
		while((s=prodInfos.size()%3)!=0){
			prodInfos.add(prod);
		}
		jsonString = null ;
		info = null ;
		brand = null ;
		prods = null ;
		return prodInfos;
	
	}
	
	
	
	public  static void setBrands(List<Brand> mbrands,List<ProdAbstract> prodsInfo){
		brands =mbrands ;
		prods.put(MobileMainFragmentActivity.HOT_PROD, prodsInfo);
		new DownLaodData().execute("");
	}
	
	
	public static void clearCaches(){
		if(brands!=null&&!brands.isEmpty()){
			brands.clear() ;
		}
		if(prods!=null&&!prods.isEmpty()){
			prods.clear();
		}
	}
	
	
	public static class DownLaodData extends AsyncTask<String, String, String> {

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			if(brands!=null&&!brands.isEmpty()&&brands.size()>0){
				for (Brand brand : brands) {
					List<ProdAbstract> prodsList = prods.get(brand.brand_name);
					if (prodsList == null || prodsList.isEmpty()) {
						String urlPath = HttpUtils.BASE_URI + "category?b="
								+ brand.brand_id;
						try {
							prodsList = MobileAdapter.getProds(urlPath);
							prods.put(brand.brand_name, prodsList);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					if(brands==null||brands.isEmpty()||brands.size()<1){
						break ;
					}
				}
			}
			return null;
		}
		
	}
	

}
